local meibu = fk.CreateSkill{
  name = "bd__meibu",
}

Fk:loadTranslationTable{
  ["bd__meibu"] = "魅步",
  [":bd__meibu"] = "其他角色的装备牌于你的回合内进入弃牌堆后，其可以令你获得之，则你本回合不能使用【杀】；"..
    "你的基本牌于其他角色的回合内进入弃牌堆后，你可以令其获得之，则其本回合不能使用【杀】。",
  ["#bd__meibu"] = "魅步：你可以令 %dest 获得进入弃牌堆的牌且本阶段不能使用【杀】",
  ["@@bd__meibu_prohibit-phase"] = "魅步",
}

meibu:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local current = room:getCurrent()
    local isMyTurn = current == player
    
    -- 获取当前事件链信息，用于判断处理区牌
    local move_event = room.logic:getCurrentEvent()
    local use_event = move_event.parent
    local card_event_info = {}
    
    -- 收集使用/响应/拼点事件中的牌和角色信息
    if use_event ~= nil then
      if use_event.event == GameEvent.UseCard or use_event.event == GameEvent.RespondCard then
        local use = use_event.data
        local card_ids = room:getSubcardsByRule(use.card)
        for _, cardId in ipairs(card_ids) do
          card_event_info[cardId] = use.from
        end
      elseif use_event.event == GameEvent.Pindian then
        local pindianData = use_event.data
        if (isMyTurn and pindianData.from ~= player) or (not isMyTurn and pindianData.from == player) then
          local card_ids = room:getSubcardsByRule(pindianData.fromCard)
          for _, cardId in ipairs(card_ids) do
            card_event_info[cardId] = pindianData.from
          end
        else
          for to, result in pairs(pindianData.results) do
            local card_ids = room:getSubcardsByRule(result.toCard)
            for _, cardId in ipairs(card_ids) do
              card_event_info[cardId] = to
            end
            break
          end
        end
      end
    end
    -- 按角色分组收集牌
    local cards_from_owner = {}
    for _, p in ipairs(room:getAlivePlayers(false)) do
      cards_from_owner[p.id] = {}
    end
    
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        -- 手牌/装备区移动
        if move.from then
          for _, info in ipairs(move.moveInfo) do
            if (room.current == player and Fk:getCardById(info.cardId).type == Card.TypeEquip)
              or (room.current ~= player and Fk:getCardById(info.cardId).type == Card.TypeBasic) then
              table.insert(cards_from_owner[move.from.id], info.cardId)
            end
          end
        end
      
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.Processing and card_event_info[info.cardId] then
            if (room.current == player and Fk:getCardById(info.cardId).type == Card.TypeEquip)
              or (room.current ~= player and Fk:getCardById(info.cardId).type == Card.TypeBasic) then
              local owner = card_event_info[info.cardId]
              table.insert(cards_from_owner[owner.id], info.cardId)
              end
          end
        end
      end
    end
    for _, p in ipairs(room:getAlivePlayers(false)) do
      cards_from_owner[p.id] = room.logic:moveCardsHoldingAreaCheck(cards_from_owner[p.id])
    end
    
    -- 触发技能效果
    if room.current == player then
      for _, p in ipairs(room:getOtherPlayers(player, false)) do
        if #cards_from_owner[p.id] > 0 then
          event:setCostData(self, {target = p, cards = cards_from_owner[p.id]})
          self:doCost(event, target, player, data)
        end
      end
    else
      if #cards_from_owner[player.id] > 0 then
        event:setCostData(self, {target = player, cards = cards_from_owner[player.id]})
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local cost_data = event:getCostData(self)    
    return player.room:askToSkillInvoke(cost_data.target, {
      prompt = "#bd__meibu::"..player.room.current.id,
      skill_name = meibu.name,
    })
  end,
  
  on_use = function(self, event, target, player, data)
    local cost_data = event:getCostData(self)
    local room = player.room
    local cards = cost_data.cards
    room:obtainCard(room.current, cards, true, fk.ReasonPrey, player, meibu.name)
    room:addPlayerMark(room.current, "@@bd__meibu_prohibit-phase", 1)
  end,
})

meibu:addEffect("prohibit", {
  prohibit_use  =function (self, player, card)
    return player:getMark("@@bd__meibu_prohibit-phase") > 0 and card.trueName == "slash"
  end
})

return meibu
